package com.augurit.common.system.service.org.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.common.dao.AtHomepageConfigDao;
import com.augurit.common.entity.AtHomepageConfig;
import com.augurit.common.system.convert.AtHomepageConfigConverter;
import com.augurit.common.system.service.org.IAtHomepageConfigService;
import com.augurit.common.system.web.org.form.AtHomepageConfigForm;
import com.augurit.common.util.PageUtils;

/**
 * 系统首页配置逻辑实现类
 */
@Service
@Transactional
public class AtHomepageConfigServiceImpl implements IAtHomepageConfigService {
	
	// DAO 引用
	@Autowired
	private AtHomepageConfigDao atHomepageConfigDao; //应用DAO类

	/**
	 * 获取启用的首页配置Form对象
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<AtHomepageConfigForm> getActiveHomepages(){
		return atHomepageConfigDao.getActiveHomepages();
	}
	
	/**
	 * 根据首页编号获取首页配置信息
	 * @param homeCode
	 * @return
	 */
	@Transactional(readOnly = true)
	public AtHomepageConfigForm getActiveHompageConfigByCode(String homeCode){
		String hql = "from AtHomepageConfig ps where ps.isActive='1' and ps.homepageCode=?";
		AtHomepageConfig entity = atHomepageConfigDao.findUnique(hql, homeCode);
		return AtHomepageConfigConverter.convertToForm(entity);
	}
	
	/**
	 * 根据首页编号获取首页配置信息
	 * @param homeCode
	 * @return
	 */
	@Transactional(readOnly = true)
	public AtHomepageConfigForm getHompageConfigByCode(String homeCode){
		String hql = "from AtHomepageConfig ps where ps.homepageCode=?";
		AtHomepageConfig entity = atHomepageConfigDao.findUnique(hql, homeCode);
		return AtHomepageConfigConverter.convertToForm(entity);
	}
	
	@Transactional(readOnly = true)
	public AtHomepageConfigForm get(Long id){
		return atHomepageConfigDao.getForm(id);
	}

	/**
	 * 保存业务字典项Form对象数组
	 * @param forms Form对象数组
	 */
	public void save(AtHomepageConfigForm... forms){
		if(forms != null){
			for(AtHomepageConfigForm form : forms)
				atHomepageConfigDao.save(form);
		}
	}
	
	public void save(List<AtHomepageConfigForm> forms){
		if(forms != null && forms.size() > 0){
			for(AtHomepageConfigForm form : forms)
				atHomepageConfigDao.save(form);
		}
	}

	public void delete(Long id) {
		if(id != null)
			atHomepageConfigDao.delete(id);
	}
	
	/**
	 * 根据主键ID集合删除多个业务字典项对象
	 * @param ids 业务字典项主键ID集合
	 */
	public void delete(Long[] ids) {
		if(ids != null && ids.length > 0)
			atHomepageConfigDao.delete(ids);
	}

	/**
	 * 获取所有已启用的系统首页
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<AtHomepageConfigForm> getAllActive(){
		String hql = "from AtHomepageConfig ps where ps.isActive='1' order by ps.homepageCode";
		List<AtHomepageConfig> list = atHomepageConfigDao.find(hql);;
		return AtHomepageConfigConverter.convertToFormList(list);
	}
	
	/**
	 * 分页获取所有系统首页
	 * @param page
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<AtHomepageConfigForm> getAll(Page<AtHomepageConfigForm> page){
		// 建立新的分页对象
		Page<AtHomepageConfig> pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		String hql = "from AtHomepageConfig ps order by ps.isActive desc";
		atHomepageConfigDao.findPage(pg, hql);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<AtHomepageConfigForm> list = AtHomepageConfigConverter.convertToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
}
